package great.easychat.help.config;

import com.pince.renovace2.Util.RenovaceLogUtil;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * @author superman
 * 网络请求日志拦截器
 */
public class CustomLogInterceptor implements Interceptor {

    private HttpLoggingInterceptor httpLoggingInterceptor;

    public CustomLogInterceptor() {
//        httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
//            @Override
//            public void log(String message) {
//                LogUtil.d("netHttp--> "+message);
//            }
//        });
//        httpLoggingInterceptor.setLevel(RenovaceLogUtil.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.NONE);
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
//        return httpLoggingInterceptor.intercept(chain);

        final Request request = chain.request();
        final Response response = chain.proceed(request);
        final ResponseBody body = response.body();
        final String bodyString = body.string();
        RenovaceLogUtil.logD(String.format("result\nUrl=%s\nbody=%s", request.url(), bodyString));

        return response.newBuilder()
                .headers(response.headers())
                .body(ResponseBody.create(body.contentType(), bodyString))
                .build();
    }
}